package com.threeti.jiayusms.web.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.WriteException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.threeti.jiayusms.domain.model.legacy.CommunicationLog;
import com.threeti.jiayusms.domain.service.communicationlog.CommunicationLogQueryService;
import com.threeti.jiayusms.domain.service.communicationlog.CommunicationLogUpdateService;
import com.threeti.jiayusms.framework.util.DynaSearchCriteria;
import com.threeti.jiayusms.framework.util.PagedListHandler;
import com.threeti.jiayusms.framework.web.PagedQueryControllerAdapter;
import com.threeti.jiayusms.web.form.LogTemplateForm;

@Controller
@SessionAttributes("LogTemplateForm.LogTemplateDynaCriteria")
public class CommunicationLogTemplateMainController {

	@Autowired
	private CommunicationLogQueryService queryService;
	
	  @Autowired
	  private CommunicationLogUpdateService updateService;
	
	@RequestMapping(value="/logTemplateMain.html")
	  public String enterLogTemplateMain(@ModelAttribute LogTemplateForm form, HttpServletRequest request,Map modelMap) {
	    modelMap.put(LogTemplateForm.SESSION_DYNA_CRITERIA_KEY, (form.buildDynaSearchCriteriaBy("com.threeti.jiayusms.interfaces.LogTemplateDynaCriteria")));
	    
	    PagedQueryControllerAdapter pagedHelper = new PagedQueryControllerAdapter();
	    int[] index = pagedHelper.getIndex(request);    
	    PagedListHandler<CommunicationLog> pagedLogTemplates = queryService.findAllLogTemplatesByPage(index[0], index[1]);
	    pagedHelper.processPage(pagedLogTemplates, request);
	    
	    modelMap.put("logTemplates", pagedLogTemplates.getPageList());
	    
	    return "report/logTemplateMain";
	  }
	
	@RequestMapping(value="/removeLogTemplate.html", method = RequestMethod.POST)
	  public String removeLogTemplate(@RequestParam("keys") String pks,
	                            Map modelMap, HttpServletRequest request) {
	    String[] logTemplateIds = pks.split(",");

	    for (int i = 0; i < logTemplateIds.length; i++) {
	      updateService.removeCommunicationLogBy(Long.valueOf(logTemplateIds[i]));
	    }
	    return "redirect:/logTemplateMain.html";
	  }
	
	  @RequestMapping(value="/searchLogTemplateByPage.html")
	  public String searchLogTemplate(HttpServletRequest request, @ModelAttribute LogTemplateForm form, Map modelMap) {

	  	DynaSearchCriteria dynaSearchCriteria = null;
	  	
	    PagedQueryControllerAdapter pagedHelper = new PagedQueryControllerAdapter();
	  	if (pagedHelper.isAnyPageSearch(request)) {
	  		dynaSearchCriteria = (DynaSearchCriteria)modelMap.get(LogTemplateForm.SESSION_DYNA_CRITERIA_KEY);
	  	} else {
	      modelMap.put(LogTemplateForm.SESSION_DYNA_CRITERIA_KEY, (dynaSearchCriteria = form.buildDynaSearchCriteriaBy("com.threeti.jiayusms.interfaces.LogTemplateDynaCriteria")));  		
	  	}
	    int[] index = pagedHelper.getIndex(request);    
	    PagedListHandler<CommunicationLog> pagedLogTemplates = queryService.findLogTemplatesByPage(dynaSearchCriteria, index[0], index[1]);
	    pagedHelper.processPage(pagedLogTemplates, request);
	    
	    modelMap.put("logTemplates", pagedLogTemplates.getPageList());
	    
	    return "report/logTemplateMain";
	  }
	  
	  @RequestMapping(value="/exportLogTemplateByPage.html")
	  public void exportLogTemplate(HttpServletRequest request, @ModelAttribute LogTemplateForm form, Map modelMap,HttpServletResponse response) throws WriteException, IOException {

	  	DynaSearchCriteria dynaSearchCriteria = (DynaSearchCriteria)modelMap.get(LogTemplateForm.SESSION_DYNA_CRITERIA_KEY);
	  	
//	    PagedQueryControllerAdapter pagedHelper = new PagedQueryControllerAdapter();
//	  	if (pagedHelper.isAnyPageSearch(request)) {
//	  		dynaSearchCriteria = (DynaSearchCriteria)modelMap.get(LogTemplateForm.SESSION_DYNA_CRITERIA_KEY);
//	  	} else {
//	      modelMap.put(LogTemplateForm.SESSION_DYNA_CRITERIA_KEY, (dynaSearchCriteria = form.buildDynaSearchCriteria()));  		
//	  	}
//	    int[] index = pagedHelper.getIndex(request);
	    List<CommunicationLog> LogTemplatesList = queryService.findLogTemplatesByList(dynaSearchCriteria);
	  
	    request.setAttribute("export", LogTemplatesList);
	    ExportSmsLogController.exportSmsHistory(request, response);
	    
	    //return searchLogTemplate(request,form,modelMap);
	  }
	
}
